package cn.telling.tianlianproduct.otherservice.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import cn.telling.tianlianproduct.common.util.TianlianConstance;
import cn.telling.tianlianproduct.otherservice.dao.BuyerMapper;
import cn.telling.tianlianproduct.otherservice.service.IBuyer;
import cn.telling.tianlianproduct.otherservice.vo.BuyerInfoVo;
import cn.telling.tianlianproduct.productrelease.dao.ShopProductMapper;
import cn.telling.tianlianproduct.productrelease.dao.SupplyArea0Mapper;
import cn.telling.tianlianproduct.productrelease.vo.ShopProductVo;
import cn.telling.tianlianproduct.productrelease.vo.SupplyArea0Vo;

/**
 * 
 * @ClassName: BuyerImpl 
 * @Description: 买家业务接口实现
 * @author 陈赟 
 * @date 2015年9月29日 下午2:16:15 
 *
 */
@Service("buyerOtherService")
public class BuyerImpl implements IBuyer {
    
    @Autowired
    @Qualifier("buyerMapper")
    private BuyerMapper buyerMapper;
    
    @Autowired
    @Qualifier("shopProductMapper")
    private ShopProductMapper shopProductMapper;

    @Autowired
    @Qualifier("supplyArea0Mapper")
    private SupplyArea0Mapper supplyArea0Mapper;

    @Override
    public List<Map<String, Object>> getBuyersByAreaIds(String[] areaInfos, String supplyId, String productId) {
        List<Map<String, Object>> buyerMapList = new ArrayList<Map<String, Object>>();
        
        if(areaInfos == null || areaInfos.length == 0) {
            return buyerMapList;
        }
        
        String[] areaIds = new String[areaInfos.length];
        for(int i=0; i<areaInfos.length; i++) {
            areaIds[i] = areaInfos[i].split(":")[0];
        }
        String areaLevel = areaInfos[0].split(":")[2];
        
        List<BuyerInfoVo> buyerList = null;
        // 如果是全国、省级，则按照PROVINCE字段查找买家
        if(areaLevel.equals(TianlianConstance.AREA_LEAF_NATION) || areaLevel.equals(TianlianConstance.AREA_LEAF_PROVINCE)) {
            buyerList = buyerMapper.findBuyersByProvince(areaIds);
        } else if (areaLevel.equals(TianlianConstance.AREA_LEAF_CITY)) { // 如果是市级，则按照CITY字段查找买家
            buyerList = buyerMapper.findBuyersByCity(areaIds);
        } else if (areaLevel.equals(TianlianConstance.AREA_LEAF_COUNTY)) { // 如果是区、县级，则BUYERINFO表按COUNTY字段查；COMMON_BUYERINFO表按DISTRICT字段查
            buyerList = buyerMapper.findBuyersByCounty(areaIds);
        }
        
        if(buyerList == null || buyerList.isEmpty()) {
            return buyerMapList;
        }
        
        // 查出该供应商对该产品的定向到买家的发布记录
        List<SupplyArea0Vo> supplyArea0List = null;
        ShopProductVo shopProductVo = shopProductMapper.findShopProductBySupplyIdAndProductId(supplyId, productId);
        if(shopProductVo != null) {
            supplyArea0List = supplyArea0Mapper.findSupplyArea0ListByShopProductId(shopProductVo.getId().toString());
        }
        
        for(String areaId : areaIds) {
            Map<String, Object> map = new HashMap<String, Object>();
            List<BuyerInfoVo> temp = new ArrayList<BuyerInfoVo>();
            for(BuyerInfoVo buyerInfoVo : buyerList) {
                if(areaId.equals(buyerInfoVo.getSelectedAreaId())) {
                    // 如果该买家存在发布记录，则把是否发布过标识置为true
                    if(supplyArea0List != null && !supplyArea0List.isEmpty()) {
                        for(SupplyArea0Vo supplyArea0 : supplyArea0List) {
                            if(buyerInfoVo.getBuyerId().equals(supplyArea0.getCustomId())) {
                                buyerInfoVo.setReleased(true);
                                break;
                            }
                        }
                    }
                    if(!buyerInfoVo.isReleased()) {
                        temp.add(buyerInfoVo);
                    }
                }
            }
            map.put("areaId", areaId);
            map.put("buyers", temp);
            
            buyerMapList.add(map);
        }
        return buyerMapList;
    }

}
